home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: presby.edu!jtbell
- From: jtbell@presby.edu (Jon Bell)
- Subject: Re: Class
- Message-ID: <DnpEHt.BH3@presby.edu>
- Date: Sun, 3 Mar 1996 18:10:40 GMT
- References: <4hchoh$ngh@nova.umuc.edu>
- Organization: Presbyterian College, Clinton, South Carolina USA
-
- [posted and e-mailed]
-
- Steve Russell <srussell@nova.umuc.edu> wrote:
- >1. Can I read from and output to files directly from the class? Can
- > I include<fstream.h> in the class? Do I need to include other things
- > like iostream.h and other various libraries for library functions?
-
- Yes, you can #include header files like iostream.h, fstream.h, math.h,
- etc. In fact, you *must* #include them in the header file for your
- class, if you use any of that stuff in your class, and you want it to
- compile.
-
- >3. Is this the right way to right a file for a class in unix:
- >
- > //FILE: EmpRecords.h
- > // MANIPULATES EMPLOYEE RECORDS
- > #ifdef EMPLOYEERECORDS_H_
- > #define EMPLOYEERECORDS_H_
- >
- > // class definition, and member function definitions
- > ^^^^^^^^^^^^^^^^^^^^^^^^^^^ <-- NO!
- > #endif//end of EmpRecords.h
-
- EmpRecords.h normally contains only the class definition, which in turn
- contains prototypes for the member functions. The member functions are
- normally defined in a separate file, EmpRecords.cc or EmpRecords.cp,
- which #includes EmpRecords.h, and which you usually compile separately
- from the main program. On our system, using g++, it might go something
- like this:
-
- g++ EmpRecords.cc -c [which produces EmpRecords.o]
- g++ MyProg.cc EmpRecords.o -lm -o MyProg
- [hypothetical situation in which you need the math library, -lm]
-
- This way you need to recompile EmpRecords only if you change it.
-
- >4. Finally, my class has a struct in it. This struct has a char array in it.
- > Do I need to initialize this struct via a constructor function? If so
- > would it be sufficent to set all the attributes of the struct to zero
- > and give a dummy name to the string?
-
- If your class doesn't need to allocate memory with 'new', and you're
- happy with letting everything be initialized to zero, you can probably
- get away without a constructor. Nevertheless, it's nice to have one,
- just in case you ever want to specify initial values for the member data
- when you declare an object of that type, e.g.:
-
- EmployeeRecord
- NewHire(LastName, FirstName, Salary, ...);
-
- --
- Jon Bell <jtbell@presby.edu> Presbyterian College
- Dept. of Physics and Computer Science Clinton, South Carolina USA
-